<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <h1>Hibernation Backstage Management System</h1>
      </div>
      <div>
        <el-popover
          trigger="hover"
        >
          <el-link :underline="false" @click="goBasicinfo">个人中心</el-link>
          <div class="slider"></div>
          <el-link type="danger" :underline="false" @click="logout">退出登录</el-link>
          <el-avatar src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
                     slot="reference"></el-avatar>
        </el-popover>
      </div>
    </el-header>
    <el-container>
      <div
        class="toggle-button"
        @click="toggleCollapse"
      >|||
      </div>
      <el-aside :width="isCollapse ? '68px':'200px'">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="getActivePath"
        >
          <el-submenu v-for="item in menuList" :key="item.id" :index="'/' + item.path">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span slot="title">{{ item.nav_name }}</span>
            </template>
            <el-menu-item
              v-for="subItem in item.children"
              :key="subItem.id"
              :index="'/' + subItem.path"
              @click="saveNavState('/' + subItem.path)"
            >
              {{ subItem.nav_name }}
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-main>
          <router-view></router-view>
        </el-main>
        <el-footer>Copyright © 歆垚 2022 备案号：冀ICP备xxxxxxxx号-1</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menuList: [],
      isCollapse: false,
      activePath: '',
      auth: 1,
      iconsObj: {
        1: 'el-icon-location',
        2: 'el-icon-user-solid',
        3: 'el-icon-document',
        4: 'el-icon-setting'
      }
    }
  },
  computed: {
    getActivePath () {
      return this.$store.state.activePath
    }
  },
  created () {
    this.$router.push(this.$store.state.activePath)
    this.getMenuList()
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/')
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('/menulist')
      this.menuList = res
      if (this.$store.state.auth !== 0) {
        return this.menuList.splice(1, 1)
      } else {
        this.menuList = res
      }
    },
    saveNavState (activePath) {
      this.$store.commit('changActivePath', activePath)
      this.activePath = activePath
    },
    goBasicinfo () {
      this.$router.push('/basicinfo')
      this.$store.state.activePath = '/basicinfo'
    }
  }
}
</script>
<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #ffffff;
    font-size: 20px;

    > div {
      display: flex;
      align-items: center;

      .el-avatar {
        margin-right: 20px;
      }

      h1 {
        margin-left: 20px;
      }
    }
  }

  .el-popover {
    .el-link {
      font-size: 25px;
      margin-left: 15px;
      padding: 8px 10px;
    }

    .slider {
      height: 2px;
      background-color: #333;
    }
  }

  .el-aside {
    background-color: #333744;

    .el-menu {
      border: none;
    }
  }

  .el-main {
    background-color: #EAEDF1;
  }

  .el-footer {
    background-color: #373d41;
    color: #fff;
    text-align: center;
    line-height: 65px;
  }

  .toggle-button {
    background-color: #4A5064;
    font-size: 12px;
    line-height: 800px;
    color: #ffffff;
    text-align: center;
    padding: 0 5px;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
